<?php
/**
 * 商户(公司)管理
 * @author tangbl
 *
 */
class CompanyAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 商户登陆
     */
    public function login(){
    	
    	$username = $this->_get("username","trim","");
    	//$jobno    = $this->_get("jobno"   ,"trim","");
    	$password = $this->_get("password","trim","");
    	
    	$model = D("Admin");
    	$map   = array("username"=>$username, "password"=>myencrypt($password));
    	$rows  = $model->where($map)->find();
    	
    	if($rows['id']){
    		if($rows['status']==1){
    			$this->success($rows);
    		}else{
    			$this->error("帐号已被冻结，请联系管理人员");
    		}
    	}else{
    		$this->error("帐号不存在或密码错误");
    	}
    }
    
    /**
     * 根据对应的条件查询商户
     * @access public
     * @param  int    $id  商户id
     * @return array
     */
    public function find(){
    
    	$id = $this->_get("id","intval",0);
    
    	$model = D("Company");
    	$map   = array("id"=>$id);
    	$data  = $model->where($map)->find();
    
    	$this->success($data);
    }
    
    /**
     * 添加商户
     * @access public
     * @return array
     */
    public function add(){
    	
    	$param = array(
    		"return" => false,
    	);
    	$company = parent::do_add("Company",$param);
    	
    	if($company){
    		
    		$array = array("company"=>$company);
    		$param = array(
    			"arr"    => $array,
    			"before" => "admin_add_before",
    		);
    		$company = parent::do_add("Admin", $param);
    	}else{
    		$this->error("添加商户失败");
    	}
    }
    
    /**
     * 修改商户
     * @access public
     * @return array
     */
    public function edit(){

    	$param = array(
    		"after" => "Company_edit_after"
    	);
    	parent::do_edit("Company", $param);
    }
    
    /**
     * 商户列表
     * @access public
     * @param  string $where 查询条件
     * @return array
     */
    public function index(){
    
    	$desc    = $this->_get("orderDirection","trim","desc");;
    	$order   = $this->_get("orderName","trim","addtime");
    	 
    	$model = D("Company");
    	$list  = $model->where($where)->order($order.' '.$desc)->select();
    	 
    	$this->success($list);
    }
    
    /**
     * 分页商户列表
     * @access public
     * @param  string $where 查询条件
     * @return array
     */
    public function page(){

    	$desc    = $this->_get("orderDirection","trim","desc");;
		$order   = $this->_get("orderName","trim","addtime");
		$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
		$nowPage = $this->_get("p","intval",1);
    	
    	$model = D("Company");
    	$count = $model->count();
    	$list  = $model->where($where)->order($order.' '.$desc)
    			 ->page($nowPage.','.$pageNum)->select();
    	
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    }
    
    /**
     * 删除用户
     */
    public function del(){
    	
    	$val = $this->_req("val","trim","");
    	
    	if($val){
    		
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    			$data = M("Admin")->delete($v);
    		}
    	}
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("请选择您要删除的用户");
    	}
    }

   
}